package com.ruoyi.system.service.honor.impl;

import java.util.List;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.honor.studenthonor.Student;
import com.ruoyi.system.domain.honor.studenthonor.StudentAchRequestVO;
import com.ruoyi.system.domain.honor.studenthonor.StudentAchievements;
import com.ruoyi.system.mapper.honor.StudentAchievementsMapper;
import com.ruoyi.system.mapper.honor.StudentMapper;
import com.ruoyi.system.service.honor.IStudentAchievementsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import static com.ruoyi.common.utils.SecurityUtils.getUserId;

/**
 * 在校生代性成果Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-08-05
 */
@Service
public class StudentAchievementsServiceImpl implements IStudentAchievementsService
{
    @Autowired
    private StudentAchievementsMapper studentAchievementsMapper;
    @Autowired
    private StudentMapper studentMapper;
    /**
     * 查询在校生代性成果
     * 
     * @param id 在校生代性成果主键
     * @return 在校生代性成果
     */
    @Override
    public StudentAchRequestVO selectStudentAchievementsById(Long id)
    {
        StudentAchievements studentAchievements = studentAchievementsMapper.selectStudentAchievementsById(id);
        Student student = studentMapper.selectStudentById(studentAchievements.getStudentId());
        StudentAchRequestVO studentAchRequestVO = new StudentAchRequestVO();
        studentAchRequestVO.setStudentId(student.getId());
        studentAchRequestVO.setStudentName(student.getStudentName());
        studentAchRequestVO.setEnrollmentTime(student.getEnrollmentTime());
        studentAchRequestVO.setDegreeType(student.getDegreeType());
        studentAchRequestVO.setLearningMethod(student.getLearningMethod());
        studentAchRequestVO.setAchievementName(studentAchievements.getAchievementName());
        studentAchRequestVO.setAchievementCategory(studentAchievements.getAchievementCategory());
        studentAchRequestVO.setObtainmentTime(studentAchievements.getObtainmentTime());
        studentAchRequestVO.setAchievementBrief(studentAchievements.getAchievementBrief());
        studentAchRequestVO.setAttachment(studentAchievements.getAttachment());
        studentAchRequestVO.setTeacherId(studentAchievements.getTeacherId());
        studentAchRequestVO.setTeacherName(studentAchievements.getTeacherName());
        studentAchRequestVO.setStudentParticipation(studentAchievements.getStudentParticipation());
        studentAchRequestVO.setId(studentAchievements.getId());
        return studentAchRequestVO;
    }

    /**
     * 查询在校生代性成果列表
     * 
     * @param studentAchievements 在校生代性成果
     * @return 在校生代性成果
     */
    @Override
    public List<StudentAchievements> selectStudentAchievementsList(StudentAchievements studentAchievements)
    {
        //如果是老师，则只显示自己上传的
        if(studentAchievements.getCurrentRole()!=null&&studentAchievements.getCurrentRole().equals("teacher")){
            studentAchievements.setTeacherId(getUserId());
        }
        //如果是部门，则显示老师是系里的所有
        if(studentAchievements.getCurrentRole()!=null&&studentAchievements.getCurrentRole().equals("department")){
            studentAchievements.setDeptId(SecurityUtils.getDeptId());
        }
        List<StudentAchievements> list = studentAchievementsMapper.selectStudentAchievementsList(studentAchievements);
        for (StudentAchievements ra : list) {
            //获取学生名称
            Student student = studentMapper.selectStudentById(ra.getStudentId());
            ra.setDegreeType(student.getDegreeType());
            ra.setLearningMethod(student.getLearningMethod());
            ra.setEnrollmentTime(student.getEnrollmentTime());
            ra.setStudentName(student.getStudentName());
        }
        return list;
    }

    /**
     * 新增在校生代性成果
     * 
     * @param studentAchievements 在校生代性成果
     * @return 结果
     */
    @Override
    public int insertStudentAchievements(StudentAchRequestVO studentAchievements)
    {
        StudentAchievements ra = new StudentAchievements() ;
        // 判断学生id是否为空
        if (studentAchievements.getStudentId() != null) {//不为空 说明之前有这个学生
            ra.setStudentId(studentAchievements.getStudentId());
            ra.setAchievementBrief(studentAchievements.getAchievementBrief());
            ra.setAttachment(studentAchievements.getAttachment());
            ra.setTeacherId(studentAchievements.getTeacherId());
            ra.setTeacherName(studentAchievements.getTeacherName());
            ra.setObtainmentTime(studentAchievements.getObtainmentTime());
            ra.setAchievementCategory(studentAchievements.getAchievementCategory());
            ra.setStudentParticipation(studentAchievements.getStudentParticipation());
            return studentAchievementsMapper.insertStudentAchievements(ra);
        }else{//为空 说明之前没有这个学生
            Student student = new Student();
            student.setStudentName(studentAchievements.getStudentName());
            student.setEnrollmentTime(studentAchievements.getEnrollmentTime());
            student.setDegreeType(studentAchievements.getDegreeType());
            student.setLearningMethod(studentAchievements.getLearningMethod());
            int result = studentMapper.insertStudent(student);
            if (result > 0) {
                Long studentId = student.getId();
                ra.setStudentId(studentId);
                ra.setAchievementBrief(studentAchievements.getAchievementBrief());
                ra.setAttachment(studentAchievements.getAttachment());
                ra.setTeacherId(studentAchievements.getTeacherId());
                ra.setTeacherName(studentAchievements.getTeacherName());
                ra.setObtainmentTime(studentAchievements.getObtainmentTime());
                ra.setAchievementCategory(studentAchievements.getAchievementCategory());
                ra.setStudentParticipation(studentAchievements.getStudentParticipation());
                return studentAchievementsMapper.insertStudentAchievements(ra);
            }
        }
        return 0;
    }

    /**
     * 修改在校生代性成果
     * 
     * @param studentAchievements 在校生代性成果
     * @return 结果
     */
    @Override
    public int updateStudentAchievements(StudentAchievements studentAchievements)
    {
        return studentAchievementsMapper.updateStudentAchievements(studentAchievements);
    }

    /**
     * 批量删除在校生代性成果
     * 
     * @param ids 需要删除的在校生代性成果主键
     * @return 结果
     */
    @Override
    public int deleteStudentAchievementsByIds(Long[] ids)
    {
        return studentAchievementsMapper.deleteStudentAchievementsByIds(ids);
    }

    /**
     * 删除在校生代性成果信息
     * 
     * @param id 在校生代性成果主键
     * @return 结果
     */
    @Override
    public int deleteStudentAchievementsById(Long id)
    {
        return studentAchievementsMapper.deleteStudentAchievementsById(id);
    }
}
